//
//  likeViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/15.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "likeViewController.h"
#import "baseCollection.h"
#import "LikeLayout.h"
#import "FirstCollectionCell.h"

#import "FirstReusableView.h"
#import "APPTools.h"
#import "ModelOfFirstDetail.h"
#import "LikeDetailViewController.h"
#import "SearchViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#import <DKNightVersion.h>
@interface likeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain)baseCollection *collection;
@property (nonatomic,retain)NSMutableArray *arrAll;//所有存放model类型的数据
@property (nonatomic,retain)NSMutableArray *arrTianSheng;//天生
@property (nonatomic,retain)NSMutableArray *arrBaiRi;//白日
@property (nonatomic,retain)NSMutableArray *arrShiShang;//时尚
@property (nonatomic,retain)NSMutableArray *arrChaoMeng;//超萌
@property (nonatomic,retain)NSMutableArray *arrGroupName;//分组名
@property (nonatomic,retain)UISearchController *search;
@end

@implementation likeViewController


//内存管理

- (void)dealloc{
    [_collection release];
    [_arrAll release];
    [_arrTianSheng release];
    [_arrBaiRi release];
    [_arrShiShang release];
    [_arrChaoMeng release];
    [_arrGroupName release];
    [_search release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self datahandler];
    [self createCollection];
    self.navigationItem.title = @"图库";
    
    //搜索
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sousuo"] style:UIBarButtonItemStylePlain target:self action:@selector(search:)];

}
#pragma mark - ------------ 创建搜索视图 ---------------
-(void)createSearch{
    
    _search = [[UISearchController alloc]initWithSearchResultsController:nil];
    //搜索结果的视图控制器
    _search.searchResultsUpdater = self;
    //确定是否变暗在底层内容搜索。
    _search.dimsBackgroundDuringPresentation = NO;
    
    //是否隐藏导航栏
    _search.hidesNavigationBarDuringPresentation = NO;
    
    //设置大小
    _search.searchBar.frame = CGRectMake(self.search.searchBar.frame.origin.x, self.search.searchBar.frame.origin.y, 44, 0);
   
    
    
    
    

}

#pragma mark - ------------ 解析数据---------------

-(void)datahandler{
  
    [APPTools GETWithURL:@"http://203.80.144.213/napi/index/groups/?app_code=gandalf&platform_version=9.3.1&app_version=6.0.6%20rv%3A156728&screen_width=320&__domain=www.duitang.com&locale=zh_CN&screen_height=568&device_platform=iPhone5%2C2&platform_name=iPhone%20OS&device_name=iPhone%205" par:nil success:^(id responseObject) {
        
        NSArray *arr = responseObject[@"data"];
        
        //整个 放5个字典 分区数
        self.arrAll = [ModelOfFirstDetail modelHandleWithArrar:arr];
        
        
        //天生购物狂
        NSArray *arr1 = arr[1][@"group_items"];
        self.arrTianSheng  = [ModelOfFirstDetail modelHandleWithArrar:arr1];
        
        //白日梦想家
        NSArray *arr2 = arr[2][@"group_items"];
        self.arrBaiRi  = [ModelOfFirstDetail modelHandleWithArrar:arr2];
        
        //时尚女魔头
        NSArray *arr3 = arr[3][@"group_items"];
        self.arrShiShang  = [ModelOfFirstDetail modelHandleWithArrar:arr3];
        
        //超萌战队
        NSArray *arr4 = arr[4][@"group_items"];
        self.arrChaoMeng  = [ModelOfFirstDetail modelHandleWithArrar:arr4];
        
        
        //分组名 出掉第一个字典
        self.arrGroupName = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            
            [self.arrGroupName addObject:dic[@"group_name"]];
        }
        if (_arrGroupName.count != 0) {
            
            [_arrGroupName removeObjectAtIndex:0];
        }
      
        [_collection reloadData];
        
        
    } filed:^(NSError *error) {
        
    }];


}





#pragma mark - ------------ 创建collectionview ---------------

- (void)createCollection{
    
    //设置每个分区内置大小
    LikeLayout *layout = [[LikeLayout alloc]init];
   // layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    self.collection = [[baseCollection alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
   
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[FirstCollectionCell class] forCellWithReuseIdentifier:@"pool1"];
    [self.collection registerClass:[FirstReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:self.collection];
    
}

#pragma mark - ------------ 按分区返回item个数 ---------------

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else if (section == 1){
        return 5;
    }else if (section == 2){
        return 4;
    }
    else{
        return 7;
    }
}

#pragma mark ------------- 返回4分区 ---------------

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return  4;
}

#pragma mark - ------------ cell赋值 ---------------

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
    FirstCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    switch (indexPath.section) {
        case 0:{
            ModelOfFirstDetail *model = _arrTianSheng[indexPath.item];
            cell.model = model;
            break;
        }
        case 1:{
            ModelOfFirstDetail *model = _arrBaiRi[indexPath.item];
            cell.model = model;
            break;
        }case 2:{
            ModelOfFirstDetail *model = _arrShiShang[indexPath.item];
            cell.model = model;
            break;
        }
        default:{
            ModelOfFirstDetail *model = _arrChaoMeng[indexPath.item];
            cell.model = model;
            break;
        }
                }
    
    return cell;
    
}

#pragma mark - ------------ 分区头赋值 ---------------

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{


    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        
        FirstReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
           header.labelHeader.text = _arrGroupName[indexPath.section];
           header.dk_backgroundColorPicker = DKColorPickerWithKey(BAR);
        return header;
    }
    return nil;
}
#pragma mark - ------------ 点击事件 ---------------

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    LikeDetailViewController *vc = [[LikeDetailViewController alloc]init];
    switch (indexPath.section) {
        case 0:{
            
            ModelOfFirstDetail *m = _arrTianSheng[indexPath.item];
            vc.strDetail = m.target;
            vc.naviTitle = m.name;
            break;
        }case 1:{
            ModelOfFirstDetail *m = _arrBaiRi[indexPath.item];
            vc.strDetail = m.target;
            vc.naviTitle = m.name;
            break;
        }case 2:{
            ModelOfFirstDetail *m = _arrShiShang[indexPath.item];
            vc.strDetail = m.target;
            vc.naviTitle = m.name;
            break;
        }
        default:{
            ModelOfFirstDetail *m = _arrChaoMeng[indexPath.item];
            vc.strDetail = m.target;
            vc.naviTitle = m.name;
        
            break;
        }
    }
    
    [self.navigationController pushViewController:vc animated:YES];

}

-(void)search:(UIBarButtonItem *)button{
    SearchViewController *vc = [[SearchViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];



}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
